<?php include './header.php';

if(isset($_POST['sub'])){
    $title = $_POST['title'];
    $en = $_POST['title_en'];
    $time = time();

    if($_FILES['pic1']['error']==0){
        $pic_conf = [
            'table' => 'about',
            'name' => 'pic1',
            'prefix' => 'nnd_'
        ];
        $up = Upload::getInstance($pic_conf);
        $up_img = $up -> downLoad();
        if($up_img['code'] == 1){
            $path = $up_img['path'];
            $dir = $up_img['dir'];
            $name = $up_img['new_name'];
            
            //缩略图参数
            $thumb_conf = [
                'img_addr' => $path,
                'path' => $dir,
                'name' => $name,
                'width' => 150,
                'height' => 102
            ];
            $th = Thumb::getInstance($thumb_conf);
            $thumb = $th->download();

            // 文字水印参数
            $str_conf = [
                'fonts' => 'fonts/STXINGKA.TTF',
                'str' => '哈哈哈',
                'img_addr' => $path,
                'img_name' => $name,
                'dir' => $dir
            ];
            $strmark = StrMark::getInstance($str_conf);
            $str_mark = $strmark->make();

            // 图片水印参数
            $img_conf = [
                'ori_img' => $path,
                'water_img' => 'upload/logo.png',
                'name' => $name,
                'dir' => $dir
            ];
            $imgmark = ImgMark::getInstance($img_conf);
            $img_mark = $imgmark->make();
        }else{
            $path = '';
        }
    }

    $values = [
        'about_title' => $title,
        'about_title_en' => $en,
        'about_img' => $path,
        'about_thumb' => $thumb,
        'about_time' => $time,
        'about_str_mark' => $str_mark,
        'about_img_mark' => $img_mark
    ];

    $res=$db->insert('about', $values);
    if($res){
        echo "<script>alert('添加成功!');window.location.href='about.php'</script>";
    }else{
        echo "<script>alert('添加失败!')</script>";
    }
}

?>
<!-- Start: Content -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrap-fileinput.css" rel="stylesheet">
<section id="content">
    <div id="topbar" class="affix">
        <ol class="breadcrumb">
            <li><a href="#"><span class="glyphicon glyphicon-home"></span></a></li>
            <li class="active">添加</li>
        </ol>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-md-10 col-lg-10 center-column">
                <form action="" method="post" class="cmxform" enctype='multipart/form-data'>
                    <div class="panel">
                        <div class="panel-heading">
                            <div class="panel-title">添加</div>
                            <div class="panel-btns pull-right margin-left">
                                <a href="#" onclick="window.history.back();" class="btn btn-default btn-gradient dropdown-toggle"><span class="glyphicon glyphicon-chevron-left"></span></a>
                            </div>
                        </div>
                        <div class="panel-body">
                            <div class="col-md-7">
                                <div class="form-group">
                                    <div class="input-group"><span class="input-group-addon">标题</span>
                                        <input type="text" name="title" class="form-control" required>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="input-group"><span class="input-group-addon">英文标题</span>
                                        <input type="text" name="title_en" class="form-control">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <!-- <input type="hidden" name="MAX_FILE_SIZE" id="" value="1048576"> -->
                                    <div class="fileinput fileinput-new" data-provides="fileinput" id="exampleInputUpload">
                                        <div class="fileinput-new thumbnail" style="width: 200px;height: auto;max-height:150px;">
                                            <img id='picImg' style="width: 100%;height: auto;max-height: 140px;" src="images/noimage.png" alt="" />
                                        </div>
                                        <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"></div>
                                        <div>
                                            <span class="btn btn-primary btn-file">
                                                <span class="fileinput-new">选择文件</span>
                                                <span class="fileinput-exists">换一张</span>
                                                <input type="file" name="pic1" id="picID" accept="image/gif,image/jpeg,image/x-png" />
                                            </span>
                                            <a href="javascript:;" class="btn btn-warning fileinput-exists" data-dismiss="fileinput">移除</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group col-md-12">
                            <script type="text/plain" id="myEditor" style="width:100%;height:200px;"><p></p></script>
                        </div>
                        <div class="col-md-7">
                            <div class="form-group">
                                <input type="submit" name="sub" value="提交" class="submit btn btn-blue">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>
<!-- End: Content -->
</div>
<!-- End: Main -->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap-fileinput.js"></script>
<link type="text/css" rel="stylesheet" href="umeditor/themes/default/_css/umeditor.css">
<script src="umeditor/umeditor.config.js" type="text/javascript"></script>
<script src="umeditor/editor_api.js" type="text/javascript"></script>
<script src="umeditor/lang/zh-cn/zh-cn.js" type="text/javascript"></script>
<script type="text/javascript">
    var ue = UM.getEditor('myEditor', {
        autoClearinitialContent: true,
        wordCount: false,
        elementPathEnabled: false,
        initialFrameHeight: 300
    });

    $(function () {
        //比较简洁，细节可自行完善
        $('#uploadSubmit').click(function () {
            var data = new FormData($('#uploadForm')[0]);
            $.ajax({
                url: 'xxx/xxx',
                type: 'POST',
                data: data,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success: function (data) {
                    console.log(data);
                    if(data.status){
                        console.log('upload success');
                    }else{
                        console.log(data.message);
                    }
                },
                error: function (data) {
                    console.log(data.status);
                }
            });
        });

    })
</script>

</body>

</html>